package com.framework.common.auto;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.framework.common.exception.ApiException;
import com.framework.common.utils.RedisUtils;
import com.framework.common.utils.SpringContextUtil;
import lombok.extern.log4j.Log4j2;

import java.util.Objects;

@Log4j2
public class ThreadLocalUtils {

    private static final TransmittableThreadLocal<TokenUserInfo> tokenHolder = new TransmittableThreadLocal<>();

    public static void setLoginid(String loginid) {
        try {
            RedisUtils redisUtils = SpringContextUtil.getBean(RedisUtils.class);
            TokenUserInfo tokenUserInfo = redisUtils.get(AuthConstant.LOGINID_PREFIX + loginid, TokenUserInfo.class);
            if (tokenUserInfo == null) {
                StpUtil.logout();
                throw NotLoginException.newInstance("", "");
            }
            tokenHolder.set(tokenUserInfo);
        } catch (Exception e) {
            log.error("setLoginid 异常：" + e.getMessage(), e);
            StpUtil.logout();
            throw NotLoginException.newInstance("", "");
        }
    }

    public static void setTokenUserInfo(TokenUserInfo tokenUserInfo) {
        tokenHolder.set(tokenUserInfo);
    }

    public static TokenUserInfo getTokenUserInfo() {
        return tokenHolder.get();
    }

    /**
     * 生产用户临时交互code 5分钟有消失
     *
     * @return
     */
    public static String getTempCode() {
        RedisUtils redisUtils = SpringContextUtil.getBean(RedisUtils.class);
        String key = IdUtil.simpleUUID();
        String token = StpUtil.getTokenValue();
        redisUtils.set("utoken_" + key, token, 300);
        return key;
    }

    public static String getTokenByTempCode(String code) {
        RedisUtils redisUtils = SpringContextUtil.getBean(RedisUtils.class);
        String token = redisUtils.get("utoken_" + code, String.class);
        if (StrUtil.isAllEmpty(token)) {
            throw new ApiException("用户code已过期");
        }
        return token;
    }


    /**
     * 获取当前用户实体
     */
    public static UserInfo getUserInfo() {
        TokenUserInfo tokenUserInfo = getTokenUserInfo();
        if (!Objects.isNull(tokenUserInfo)) {
            return tokenUserInfo.getUserInfo();
        } else {
            return null;
        }
    }

    /**
     * 获取用户id
     */
    public static Long getUserId() {
        UserInfo userInfo = getUserInfo();
        if (!Objects.isNull(userInfo)) {
            return userInfo.getId();
        } else {
            return null;
        }
    }

    /**
     * 获取用户类型
     */
    public static UserType getUserType() {
        UserInfo userInfo = getUserInfo();
        if (!Objects.isNull(userInfo)) {
            return userInfo.getUserType();
        } else {
            return null;
        }
    }

    /**
     * 获取用户的当前企业id
     */
    public static Long getCorpId() {
        UserInfo userInfo = getUserInfo();
        if (!Objects.isNull(userInfo)) {
            return userInfo.getCorpId();
        } else {
            return null;
        }
    }

    public static void remove() {
        tokenHolder.remove();
    }

}
